import { ref, watch } from "vue"

export function useLocalStorage(key, val) {
  const data = ref(val);

  if (val) {
    window.localStorage.setItem(key, JSON.stringify(val));
  } else {
    data.value = JSON.parse(window.localStorage.getItem(key))
  }

  watch(data, () => {
    window.localStorage.setItem(key, JSON.stringify(data.value))
  })

  return data
}